﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Web.Security;
using System.IO;

namespace WebTools
{
    public class EncriptHelper
    {
        #region 加密
        /// <summary>
        /// SHA加密
        /// </summary>
        /// <param name="tobeEncript">未加密字符串</param>
        /// <returns></returns>
        public static string EncryptSHA(string tobeEncript)
        {
            string str = "";
            str = FormsAuthentication.HashPasswordForStoringInConfigFile(tobeEncript, "SHA1");

            return str;
        }
        /// <summary>
        /// SHA加密转64码
        /// ERP 雄狮用户密码加密方法
        /// </summary>
        /// <param name="tobeEncript">未加密字符串</param>
        /// <returns></returns>
        public static string EncryptSHA64(string tobeEncript)
        {
            string rethash = "";

            try
            {
                System.Security.Cryptography.SHA1 hash = System.Security.Cryptography.SHA1.Create();
                System.Text.ASCIIEncoding encoder = new System.Text.ASCIIEncoding();
                byte[] combined = encoder.GetBytes(tobeEncript);
                hash.ComputeHash(combined);
                rethash = Convert.ToBase64String(hash.Hash);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

            return rethash;
        }

        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="tobeEncript">未加密字符串</param>
        /// <returns></returns>
        public static string EncryptMD5(string tobeEncript)
        {
            try
            {
                byte[] data = Encoding.GetEncoding("GB2312").GetBytes(tobeEncript);
                MD5 md5 = new MD5CryptoServiceProvider();
                byte[] OutBytes = md5.ComputeHash(data);

                string OutString = "";
                for (int i = 0; i < OutBytes.Length; i++)
                {
                    OutString += OutBytes[i].ToString("x2");
                }
                return OutString;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        } 
        #endregion

        #region 加解密

        const string KEY_64 = "CndTourW";//注意了，是8个字符，64位
        const string IV_64 = "CndTourW";
        /// <summary>
        /// 字符加密
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string Encode(string data)
        {
            byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
            byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);

            DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
            int i = cryptoProvider.KeySize;
            MemoryStream ms = new MemoryStream();
            CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateEncryptor(byKey, byIV), CryptoStreamMode.Write);
            StreamWriter sw = new StreamWriter(cst);
            sw.Write(data);
            sw.Flush();
            cst.FlushFinalBlock();
            sw.Flush();
            return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);
        }
        /// <summary>
        /// 字符解密
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string Decode(string data)
        {
            byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
            byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);

            byte[] byEnc;
            try
            {
                byEnc = Convert.FromBase64String(data);
            }
            catch
            {
                return null;
            }

            DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
            MemoryStream ms = new MemoryStream(byEnc);
            CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateDecryptor(byKey, byIV), CryptoStreamMode.Read);
            StreamReader sr = new StreamReader(cst);
            return sr.ReadToEnd();
        }

        #endregion
    }
}
